package com.xjie.mall.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjie.mall.goods.mapper.BrandMapper;
import com.xjie.mall.goods.model.Brand;
import com.xjie.mall.goods.model.Category;
import com.xjie.mall.goods.service.BrandService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper,Brand>  implements BrandService {

    @Autowired
    private BrandMapper brandMapper;

    /**
     * 多条件查询
     * @param brand
     * @return
     */
    @Override
    public List<Brand> queryList(Brand brand) {
        //多条件构造器
        QueryWrapper<Brand> queryWrapper = new QueryWrapper<Brand>();
        if(!StringUtils.isEmpty(brand.getName()))
        {
            queryWrapper.like("name",brand.getName());//like是模糊查询
        }

        if(!StringUtils.isEmpty(brand.getName()))
        {
            queryWrapper.eq("initial",brand.getId());//eq是等值查询
        }
        return this.baseMapper.selectList(queryWrapper);
    }

    /**
     * 分页查询
     * @param currentPage
     * @param size
     * @param brand
     * @return
     */
    @Override
    public Page<Brand> queryPageList(Long currentPage, Long size, Brand brand) {
        //封装查询条件
        Page<Brand> page = this.baseMapper.selectPage(
                new Page<Brand>(currentPage,size),
                new QueryWrapper<Brand>()
                        .like("name",brand.getName())
        );
        return page;
    }

    @Override
    public List<Brand> queryBrandIds(Integer id) {


        //1.根据分类id查询品牌列表
        List<Integer> brandIds = brandMapper.queryBrandIds(id);

        //2.根据品牌列表查询品牌名称列表
        List<Brand> brandList = baseMapper.selectBatchIds(brandIds);


        return brandList;
    }
}
